package com.pioneer.app.sms.timeTask;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;

import com.app.util.DateTimeUtil;
import com.pioneer.app.sms.define.TypeDefine;
import com.pioneer.app.sms.define.TypeDefineAction;
import com.pioneer.app.sms.mobile.SendService;

import config.SMSConfig;


/**
 * @name :  定时发短信
 * @URL : 
 * @URL_Parameter :  
 * @author chen
 * @version : v 1.0
 * @description : 
 */
public class SMSTask extends TimerTask {
//	client.sms.SMSService service= new SMSService();
	private TypeDefine typeDef=null;
	private SendService sendService=null;
	private Date sendDate=null;
	public void run() {
//  s     
		if(null==this.sendService){
			sendSMS();
			System.out.println("定时名称="+this.typeDef.getTypeName());
		}else{
			sendService.setSMS();
		}
   }
	
	private void sendSMS(){
		List smsObjs=buildInfo();
		//循环发送,
		
	}
	
	
	private List buildInfo(){
		
		try {
			if(TypeDefineAction.TYPE_SQL.equalsIgnoreCase(this.typeDef.getTmpl())){
				Connection conn=getCon();
				String sql=this.typeDef.getSqlstr();
				String content=null;
				ResultSet rs=conn.createStatement().executeQuery(sql);
				ResultSetMetaData metaData=rs.getMetaData();
				String repCol=null;
				String toMobile=null;
				while(rs.next()){
					content=this.typeDef.getContent();
					int colNum=metaData.getColumnCount();
					for(int i=1;i<=colNum;i++){
						repCol="$$"+metaData.getColumnName(i)+"$$";
						content=replace(content,repCol,rs.getString(metaData.getColumnName(i)));
					}
					toMobile=rs.getString("mobile");
					System.out.println("发送到"+toMobile+"内容为："+content);
					SendService.sendSMS(content, toMobile, "3", "123456", SMSConfig.getInst().getSysCode(),SMSConfig.getInst().getExctenNumber());
					
				}
			}else if(TypeDefineAction.TYPE_MOBILE.equalsIgnoreCase(this.typeDef.getTmpl())){
				String mobilefile=this.typeDef.getMobilefilename();
				File file=new File(mobilefile);
				List mobiles=readMobileFile(file);
				Iterator mit=mobiles.iterator();
				while(mit.hasNext()){
					String mobile=(String)mit.next();
					SendService.sendSMS(this.typeDef.getContent(), mobile, "3", "123456", SMSConfig.getInst().getSysCode(),SMSConfig.getInst().getExctenNumber());
				}
				
			}
			
			
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	public List readMobileFile(File file){
		List mobiles=new ArrayList();
		if(null!=file && file.exists()){
			
			try {
//				FileInputStream fin=new FileInputStream(file);
				FileReader fread=new FileReader(file);
				BufferedReader reader = new BufferedReader(fread);
				String line=null;
				
				line=reader.readLine();
				int t=0;
				while(true){
					if(null!=line && !"".equals(line.trim())){
						mobiles.add(line);
					}else{
						t++;
						if(t>20)break;
					}
					line=reader.readLine();
				}
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		return mobiles;
	}
	
	private String replace(String source, String oldString,String newString){
		StringBuffer output = new StringBuffer();
		int lengthOfSource = source.length();
		int lengthOfOld = oldString.length();
		int posStart = 0;
		int pos; //
		while ( (pos = source.indexOf(oldString, posStart)) >= 0){
		output.append(source.substring(posStart, pos));
		output.append(newString);
		posStart = pos + lengthOfOld;
		}
		if (posStart < lengthOfSource){
		output.append(source.substring(posStart));
		}
		return output.toString();
}
	private Connection getCon(){
		Connection conn=null;
		try {
			Class.forName(this.typeDef.getDbDirver()).newInstance();
			String url = this.typeDef.getDbLocation();
			String user = this.typeDef.getDbUser();
			String password = this.typeDef.getDbPassword();
			conn = DriverManager.getConnection(url, user, password);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public TypeDefine getTypeDef() {
		return typeDef;
	}

	public void setTypeDef(TypeDefine typeDef) {
		this.typeDef = typeDef;
	}
	
	public SendService getSendService() {
		return sendService;
	}

	public void setSendService(SendService sendService) {
		this.sendService = sendService;
	}

	public Date getSendDate() {
		
		return sendDate;
	}

	public void setSendDate(Date sendDate) {
		long datelong=DateTimeUtil.diffTimes(sendDate, new Date());
		if(datelong<0){
			this.sendDate=new Date();
		}else{
			this.sendDate = sendDate;
		}
	}
}